/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.inftel.primeproject.managedBeans;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.inftel.primeproject.remote.UsersFacadeRemote;
import org.primefaces.model.chart.MeterGaugeChartModel;
import org.primefaces.model.chart.PieChartModel;

@ManagedBean
@RequestScoped
@SuppressWarnings("serial")
public class ChartBean implements Serializable {
    @EJB
    private UsersFacadeRemote userFacade;

    private PieChartModel usersSexModel;
    
    private MeterGaugeChartModel meterGaugeModel;
    
    private int randomNumber=140;

    public ChartBean() {
        createMeterGaugeModel(); 
    }

    public PieChartModel getUsersSexModel() {
        if (usersSexModel == null) {
            createCategoryModelSex();
        }
        return usersSexModel;
    }
    
    public MeterGaugeChartModel getMeterGaugeModel() {
        return meterGaugeModel;
    }

    public void setMeterGaugeModel(MeterGaugeChartModel meterGaugeModel) {
        this.meterGaugeModel = meterGaugeModel;
    }
   
    private void createCategoryModelSex() {
        usersSexModel = new PieChartModel(calculateUsersSexModelPieData());
    }
    
    public void generateRandomNumber(){
        randomNumber= (int) (Math.random()*200+1);
        createMeterGaugeModel();
    }
    
    private void createMeterGaugeModel() {  
        List<Number> intervals = new ArrayList<Number>(){{  
            add(20);  
            add(50);  
            add(120);  
            add(220);  
        }};  
  
        meterGaugeModel = new MeterGaugeChartModel("km/h", randomNumber, intervals);  
    }  
 
    private Map<String, Number> calculateUsersSexModelPieData() {
        
        Map<String, Number> result = new TreeMap<String, Number>();
        result.put("Male", userFacade.countBySex('M'));
        result.put("female", userFacade.countBySex('F'));
        return result;
    }
    
    
    
}
